package cn.edu.jnu.supershopper;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;
import com.google.android.material.navigation.NavigationView;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;



public class MainActivity extends AppCompatActivity implements LoginDialogFragment.LoginInputListener
{
    private DrawerLayout mDrawerLayout;
    private NavigationView mNavigationView;

    public class PageViewFragmentAdapt extends FragmentStateAdapter {
        public PageViewFragmentAdapt(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) {
            super(fragmentManager, lifecycle);
        }
        @NonNull
        @Override
        public Fragment createFragment(int position) {
            switch(position)
            {
                case 0:
                    return BookFragment.newInstance();
                case 1:
                    return ShareFragment.newInstance();
                case 2:
                    return ScanFragment.newInstance();
            }
            return BookFragment.newInstance();
        }

        @Override
        public int getItemCount() {
            return 3;
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Utils.onActivityCreateSetTheme(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activaty_main);
        mDrawerLayout = findViewById(R.id.drawer_layout);
        mNavigationView = findViewById(R.id.nav_view);
        mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                item.setChecked(true);
                switch (item.getItemId()) {
                    case R.id.item_nav_menu_load:
                        LoginDialogFragment dialog = new LoginDialogFragment();
                        dialog.show(getFragmentManager(), "loginDialog");
                        break;

                    case R.id.item_nav_menu_theme:
                        ThemeDataSaver themeDataSaver =new ThemeDataSaver();
                        int themedata= themeDataSaver.Load(MainActivity.this);
                        if(themedata == 0){
                            themedata=1;
                            themeDataSaver.Save(MainActivity.this,themedata);
                            Utils.changeToTheme(MainActivity.this,1);
                        }
                        else{
                            themedata=0;
                            themeDataSaver.Save(MainActivity.this,themedata);
                            Utils.changeToTheme(MainActivity.this,0);
                        }
                        break;

                    case R.id.item_nav_menu_num:
                        AlertDialog dialog_num=new AlertDialog.Builder(MainActivity.this)
                                .setIcon(R.drawable.check_circle)//设置图标
                                .setTitle("版本")//设置标题
                                .setMessage("当前版本为：1.0.0")//设置要显示的内容
                                .setNegativeButton("检查更新", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        Toast.makeText(MainActivity.this, "当前暂无更新版本", Toast.LENGTH_SHORT).show();
                                        dialogInterface.dismiss();//销毁对话框
                                    }
                                })
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();//销毁对话框
                                    }
                                }).create();//create（）方法创建对话框
                        dialog_num.show();//显示对话框
                        break;

                    case R.id.item_nav_menu_about:
                        AlertDialog dialog_about=new AlertDialog.Builder(MainActivity.this)
                                .setIcon(R.drawable.check_circle)//设置图标
                                .setTitle("关于：")//设置标题
                                .setMessage("该项目为作者选修移动软件开发课程时所做结课项目，仅做交流学习使用。" +
                                        "话虽如此，但我希望他能够作为一个真正有使用价值的项目，" +
                                        "我所追求的是简单高效，故项目中取消了所有二级菜单。" +
                                        "此外，本项目中留有相关接口（如登录信息等），欢迎大家交流补充")

                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();//销毁对话框
                                    }
                                }).create();//create（）方法创建对话框
                        dialog_about.show();//显示对话框
                        break;
                }
                mDrawerLayout.closeDrawers();
                return true;
            }
        });

        ViewPager2 viewPager2Book = findViewById(R.id.viewpager2_main);
        viewPager2Book.setAdapter(new PageViewFragmentAdapt(getSupportFragmentManager(),getLifecycle()));
        this.setTitle("My Library");
        TabLayout tabLayout=findViewById(R.id.layout_header);
        TabLayoutMediator tabLayoutMediator=new TabLayoutMediator(tabLayout, viewPager2Book,
                (tab, position) -> {
                    switch(position)
                    {
                        case 0:
                            tab.setText("全部图书");
                            break;
                        case 1:
                            tab.setText("快捷分享");
                            break;
                        case 2:
                            tab.setText("扫码搜书");
                            break;
                    }
                });
        tabLayoutMediator.attach();
    }


    @Override
    public void onLoginInputComplete(String username, String password)
    {
        Toast.makeText(this, "帐号：" + username + ",  密码 :" + password,
                Toast.LENGTH_SHORT).show();
    }
}